//
//  YWContactTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/5/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWContactTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWHandler.h"

@interface YWContactTableViewCell ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *sendMessageBtn;
@property (nonatomic, strong) UIButton *phoneBtn;
@end

@implementation YWContactTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"YWContactTableViewCellIdentifier";
    YWContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWContactTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
    }
    return self;
}

- (void)setUpSubviews{
    CGFloat imageSize = 40;
    CGFloat buttonW  = 40;
    CGFloat nameLabelWidth = SCREEN_WIDTH - imageSize -10*4 - buttonW*2 - 20;
    
    UIImageView *iconImageV = [[UIImageView alloc]init];
    [iconImageV setFrame:CGRectMake(10, 10, imageSize, imageSize)];
    iconImageV.layer.masksToBounds = YES;
    iconImageV.layer.cornerRadius = imageSize / 2;
    [self.contentView addSubview:iconImageV];
    _iconImageView = iconImageV;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [nameLabel setFrame:CGRectMake(CGRectGetMaxX(iconImageV.frame)+10, 10, nameLabelWidth, 40)];
    nameLabel.font = kContentFont;
    [self.contentView addSubview:nameLabel];
    _nameLabel = nameLabel;
    
    UIButton *messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageBtn setFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame)+10, 15, buttonW, 30)];
    [messageBtn setImage:[UIImage imageNamed:@"common_message"] forState:UIControlStateNormal];
    [messageBtn addTarget:self action:@selector(clickMessageBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:messageBtn];
    _sendMessageBtn = messageBtn;
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneBtn setFrame:CGRectMake(CGRectGetMaxX(messageBtn.frame)+10, 15, buttonW, 30)];
    [phoneBtn setImage:[UIImage imageNamed:@"common_reporterPhone"] forState:UIControlStateNormal];
    [phoneBtn addTarget:self action:@selector(clickPhoneBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:phoneBtn];
    _phoneBtn = phoneBtn;
    
}

- (void)setContact:(YWHandler *)contact{
    _contact = contact;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:contact.headIcon];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    self.nameLabel.text = contact.userName?:@"";
}

- (void)clickMessageBtn:(id)sender{
    if ([self.delegate respondsToSelector:@selector(clickMessageButtonEvent:)]) {
        [self.delegate clickMessageButtonEvent:_contact];
    }
}

- (void)clickPhoneBtn:(id)sender{
    if ([self.delegate respondsToSelector:@selector(clickPhoneButtonEvent:)]) {
        [self.delegate clickPhoneButtonEvent:_contact];
    }
}
@end
